package edu.spec.sys.controller;

import edu.spec.sys.model.Role;
import edu.spec.sys.model.dto.ExtGridReturn;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.exception.SysException;
import edu.spec.sys.service.iface.IRoleCommandService;
import edu.spec.sys.service.iface.IRoleService;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.List;

@Controller
@RequestMapping("/admin/role")
public class RoleController {

    @Inject
    private IRoleService roleService;
	@Inject
	private IRoleCommandService roleCommandService;

    @ResponseBody
    @RequestMapping("/roles")
    public Object listAllRole(){
        List<Role> datas = roleService.listAllRole();
        return new ExtGridReturn((long)datas.size(), datas);
    }

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Object add(Role role, BindingResult br){
		if(br.hasErrors()){
			throw new SysException("角色合法性检查失败");
		}
		//TODO 角色名称唯一性检验?
		roleService.add(role);
		return new MvcReturn(true, "添加用成功");
	}

	@ResponseBody
	@RequestMapping("/delete/{id}")
	public Object delete(@PathVariable Integer id){
		roleService.delete(id);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Object update(Role role){
		Role dbRole = roleService.load(role.getId());
		if(dbRole == null){
			throw new SysException("要更新的角色[" + role.getId() + "]不存在");
		}
		dbRole.setRoleName(role.getRoleName());
		dbRole.setDes(role.getDes());
		dbRole.setRoot(role.getRoot());
		dbRole.setStatus(role.getStatus());
		roleService.update(dbRole);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/toggleStatus/{id}")
	public Object toggleStatus(@PathVariable int id){
		Role dbRole = roleService.load(id);
		if(dbRole == null){
			throw new SysException("要修改状态的角色[" + id + "]不存在");
		}
		if(dbRole.getStatus()==1){
			dbRole.setStatus(0);
		}else {
			dbRole.setStatus(1);
		}
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/findRoleByCon")
	public Object findRoleByCon(Integer status, String con){
		Pager<Role> pr = roleService.findRoleByCon(status, con);
		return new ExtGridReturn(pr.getTotal(), pr.getDatas());
	}

	@ResponseBody
	@RequestMapping("/listRoleByUser/{id}")
	public Object listRoleByUser(@PathVariable Integer id){
		List<Role> lr = roleService.listRoleByUser(id);
		return new MvcReturn(true, lr);
	}

//	@ResponseBody
//	@RequestMapping("/copy/{id}")
//	public Object copyRole(@PathVariable int id){
//		roleService.copyRole(id);
//		return new MvcReturn(true);
//	}

	@ResponseBody
	@RequestMapping("/toggleRoleCommand")
	public Object toggleRoleCommand(@RequestParam int roleId, @RequestParam int commandId, @RequestParam boolean checked){
		if(checked){
			roleCommandService.add(roleId, commandId);
		}else{
			roleCommandService.deleteByRoleAndCommand(roleId, commandId);
		}
		return new MvcReturn(true);
	}

}
